﻿Imports System
Imports System.Text
Imports System.Collections.Generic
Imports Microsoft.VisualStudio.TestTools.UnitTesting
Imports System.Xml
Imports System.Collections

<TestClass()> Public Class ManipulandoXMLTeste

    Private testContextInstance As TestContext

    '''<summary>
    '''Gets or sets the test context which provides
    '''information about and functionality for the current test run.
    '''</summary>
    Public Property TestContext() As TestContext
        Get
            Return testContextInstance
        End Get
        Set(ByVal value As TestContext)
            testContextInstance = value
        End Set
    End Property

#Region "Additional test attributes"
    '
    ' You can use the following additional attributes as you write your tests:
    '
    ' Use ClassInitialize to run code before running the first test in the class
    ' <ClassInitialize()> Public Shared Sub MyClassInitialize(ByVal testContext As TestContext)
    ' End Sub
    '
    ' Use ClassCleanup to run code after all tests in a class have run
    ' <ClassCleanup()> Public Shared Sub MyClassCleanup()
    ' End Sub
    '
    ' Use TestInitialize to run code before running each test
    ' <TestInitialize()> Public Sub MyTestInitialize()
    ' End Sub
    '
    ' Use TestCleanup to run code after each test has run
    ' <TestCleanup()> Public Sub MyTestCleanup()
    ' End Sub
    '
#End Region

    Private Const _diretorioXMLs As String = "C:\Projetos\ArtNoi\Storage\Manipulando_XMLs\"

    <Description("É necessário fazer referências aos namespaces que não estão sendo copiados")> _
    Protected Sub BugReferencias()
        'Dim _a As Fgv.SigaDois.Servicos.AgendamentoProvaDto = Nothing
        'Dim _b As Fgv.Siga2.SpringConfig.a = Nothing
        'Dim _c As System.ServiceModel.ActionNotSupportedException = Nothing
        'Dim _d As System.Runtime.Serialization.Formatter = Nothing
    End Sub

    <TestMethod()> Public Sub Verificando_Integracao_Spring_InjecoesTeste_xml()
        Assert.IsTrue(True, "Qualquer erro aqui significa falha na integração (Spring) ou ainda algum erro no mapeamento de objetos (HBMs). Vide relatório de ERRO. assembly://Teste.SigaDois.Servicos/Teste.SigaDois.Servicos/InjecoesTeste.xml")
    End Sub

    <TestMethod()> Public Sub Nao_Existem_Triggers_Habilitadas()
        Dim _triggerXML As XmlDocument
        Dim _elementos As XmlNodeList
        Dim _msgTriggersHabilitadas As StringBuilder
        Dim _nsmgr As XmlNamespaceManager
        Dim _root As XmlElement

        _triggerXML = New XmlDocument
        _triggerXML.Load(_diretorioXMLs & "TriggersJobs.xml")

        _nsmgr = New XmlNamespaceManager(_triggerXML.NameTable)
        _nsmgr.AddNamespace("g", "http://www.springframework.net")
        _root = _triggerXML.DocumentElement
        _elementos = _root.SelectNodes("g:object/g:property[@name='triggers']/g:list/g:ref", _nsmgr)


        _msgTriggersHabilitadas = New StringBuilder
        For Each _xmlNode As XmlNode In _elementos
            _msgTriggersHabilitadas.AppendLine(_xmlNode.Attributes.Item(0).InnerText)
        Next

        Assert.IsFalse(_elementos.Count > 0, "Existem as seguintes triggers habilitadas:" & vbCrLf & _msgTriggersHabilitadas.ToString)

    End Sub

    <TestMethod()> Public Sub Existem_Triggers_Nao_permitidas()

        Dim _triggerXML As XmlDocument
        Dim _elementos As XmlNodeList
        Dim _nsmgr As XmlNamespaceManager
        Dim _root As XmlElement
        Dim _triggersPermitidas As ArrayList
        Dim _triggersNaoPermitidas As ArrayList
        Dim _msgTriggersNaoPermitidas As StringBuilder

        _triggersPermitidas = New ArrayList
        '-----Inserir aqui a lista de Triggers Permitidas----------------------------
        _triggersPermitidas.Add("JobDoEnviarParaSeCRequerimentosCertificacaoTrigger")
        '----------------------------------------------------------------------------
        _triggerXML = New XmlDocument
        _triggerXML.Load(_diretorioXMLs & "TriggersJobs.xml")

        _nsmgr = New XmlNamespaceManager(_triggerXML.NameTable)
        _nsmgr.AddNamespace("g", "http://www.springframework.net")
        _root = _triggerXML.DocumentElement
        _elementos = _root.SelectNodes("g:object/g:property[@name='triggers']/g:list/g:ref", _nsmgr)


        _triggersNaoPermitidas = New ArrayList
        For Each _xmlNode As XmlNode In _elementos
            If Not _triggersPermitidas.Contains(_xmlNode.Attributes.Item(0).InnerText) Then
                If Not _triggersNaoPermitidas.Contains(_xmlNode.Attributes.Item(0).InnerText) Then
                    _triggersNaoPermitidas.Add(_xmlNode.Attributes.Item(0).InnerText)
                End If
            End If
        Next

        _msgTriggersNaoPermitidas = New StringBuilder
        For Each _trigger As String In _triggersNaoPermitidas
            _msgTriggersNaoPermitidas.AppendLine(_trigger)
        Next

        Assert.IsTrue(_triggersNaoPermitidas.Count = 0, "Existem as seguintes triggers que não são permitidas:" & vbCrLf & _msgTriggersNaoPermitidas.ToString)

    End Sub


    <TestMethod()> Public Sub Existem_Paginas_Web_Repetidas()
        Dim _webXML As XmlDocument
        Dim _elementos As XmlNodeList
        Dim _paginas As ArrayList
        Dim _msgPaginasRepetidas As StringBuilder
        Dim _nsmgr As XmlNamespaceManager
        Dim _root As XmlElement


        _webXML = New XmlDocument
        _webXML.Load(_diretorioXMLs & "Web.xml")

        _nsmgr = New XmlNamespaceManager(_webXML.NameTable)
        _nsmgr.AddNamespace("g", "http://www.springframework.net")
        _root = _webXML.DocumentElement
        _elementos = _root.SelectNodes("g:object", _nsmgr)

        _paginas = New ArrayList
        For Each _elemento As XmlNode In _elementos
            Dim _pagina As XmlNode = _elemento.Attributes("type")
            _paginas.Add(_pagina.InnerText)
        Next


        Dim _paginasRepetidas = From t In (From t In _paginas Group t By t Into _pagina = Group Select New With {.pagina = t, .total = _pagina.Count()}) Where t.total > 1

        _msgPaginasRepetidas = New StringBuilder
        For Each _paginaRepetida In _paginasRepetidas
            _msgPaginasRepetidas.AppendLine(_paginaRepetida.pagina.ToString)
        Next

        Assert.IsTrue(_paginasRepetidas.Count = 0, "Existem as seguintes páginas repetidas no web.xml:" & vbCrLf & _msgPaginasRepetidas.ToString)

    End Sub

    <TestMethod()> Public Sub Lendo_TodosValores_Conteudo_XML_Do_TRX()

        Dim _arquivo As String = LocaisEmDisco.StorageRaiz & "TRXTeste\" & "Teste.trx"
        Dim reader As XmlTextReader = New XmlTextReader(_arquivo)
        Dim elementos As ArrayList = New ArrayList

        Do While (reader.Read())
            Select Case reader.NodeType
                Case XmlNodeType.Element
                    If reader.HasAttributes Then 'Se existirem atributos
                        While reader.MoveToNextAttribute()
                            'Pega o valor do atributo.
                            elementos.Add(reader.Value)
                        End While
                    End If
                Case XmlNodeType.Text
                    'Incluir o texto do elemento no ArrayList
                    elementos.Add(reader.Value)
            End Select
        Loop

        'Dim num As String
        'For Each num In elementos
        '    Console.WriteLine(num)
        'Next

        Assert.IsTrue(True)

    End Sub

    <TestMethod()> Public Sub Lendo_Secao_UnitTestResult_Do_TRX()

        Dim _arquivo As String = LocaisEmDisco.StorageRaiz & "Teste.trx"
        Dim reader As XmlTextReader = New XmlTextReader(_arquivo)
        Dim elementos As ArrayList = New ArrayList

        Do While (reader.Read())
            Select Case reader.NodeType
                Case XmlNodeType.Element
                    If reader.LocalName = "UnitTestResult" Then
                        Dim _filtro As String = String.Format("UnitTestResult: {0}, outcome: {1}. {2}", reader.Item("testName"), reader.Item("outcome"), reader.Item("duration"))
                        elementos.Add(_filtro)
                    End If
            End Select
        Loop
        Console.WriteLine("---------")
        Dim num As String
        For Each num In elementos
            Console.WriteLine(num)
        Next


        Assert.IsTrue(True)

    End Sub

    <TestMethod()> Public Sub Quantas_Textareas_Existe_Em_Um_Arquivo()

        Dim _arquivo As String = LocaisEmDisco.StorageRaiz & "html01.html"
        Dim reader As XmlTextReader = New XmlTextReader(_arquivo)
        Dim elementos As ArrayList = New ArrayList

        Do While (reader.Read())
            Select Case reader.NodeType
                Case XmlNodeType.Element
                    If reader.LocalName = "textarea" Then
                        Dim _filtro As String = String.Format("name:{0} id:{1} class:{2}", reader.Item("name"), reader.Item("id"), reader.Item("class"))
                        elementos.Add(_filtro)
                    End If
            End Select
        Loop
        'Console.WriteLine("---------")
        Dim num As String
        For Each num In elementos
            Console.WriteLine(num)
        Next

        Assert.IsTrue(True)

    End Sub

    <TestMethod()> Public Sub Quantos_Inputs_Existem_Em_Um_Arquivo()

        Dim _arquivo As String = LocaisEmDisco.StorageRaiz & "html01.html"
        Dim reader As XmlTextReader = New XmlTextReader(_arquivo)
        Dim elementos As ArrayList = New ArrayList

        Do While (reader.Read())
            Select Case reader.NodeType
                Case XmlNodeType.Element
                    If reader.LocalName = "input" Then
                        Dim _filtro As String = String.Format("type:{3} name:{0} id:{1} class:{2}", reader.Item("name"), reader.Item("id"), reader.Item("class"), reader.Item("type"))
                        elementos.Add(_filtro)
                    End If
            End Select
        Loop
        'Console.WriteLine("---------")
        Dim num As String
        For Each num In elementos
            Console.WriteLine(num)
        Next

        Assert.IsTrue(True)

    End Sub

    'PRIMEIRO PASSO: RECONHECIMENTO
    'LEITURA E TRANSFORMACAO
    'SOH LEMBRANDO: AUTOMACAO, A PROVA DE PERDA DE MEMORIA, AS COISAS NO LUGAR CERTO, TEMPO EH RELIGIAO
    <TestMethod()> Public Sub Quantos_TextBox_Existem_Em_Um_Arquivo()

        Dim _arquivo As String = LocaisEmDisco.StorageRaiz & "Default.aspx"
        Dim _webXML As XmlDocument
        Dim _elementos As XmlNodeList
        Dim _paginas As ArrayList
        Dim _msgPaginasRepetidas As StringBuilder
        Dim _nsmgr As XmlNamespaceManager
        Dim _root As XmlElement


        _webXML = New XmlDocument
        _webXML.Load(_arquivo)

        Throw New NotImplementedException

        _nsmgr = New XmlNamespaceManager(_webXML.NameTable)
        _nsmgr.AddNamespace("asp", "http://www.w3.org/1999/xhtml")
        _root = _webXML.DocumentElement
        _elementos = _root.SelectNodes("asp:Button", _nsmgr)

        _paginas = New ArrayList
        For Each _elemento As XmlNode In _elementos
            Dim _pagina As XmlNode = _elemento.Attributes("type")
            _paginas.Add(_pagina.InnerText)
        Next



    End Sub

End Class
